﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorkflowWizart.Lib.Utility.Costants;
using WorkflowWizart.Lib.Entities;

namespace WorkflowWizart.Lib.Utility
{
    public class ConvertHelper
    {
        public static T To<T>(object data)
        {
            T result = default(T);
            try
            {
                if (data == null || data == DBNull.Value)
                {
                    return result;
                }
                result = (T)data;
            }
            catch (Exception ex)
            {
                return result;
            }

            return result;
        }

        public static bool ToBool(object data)
        {
            bool result = false;
            if (data == null || data == DBNull.Value || !bool.TryParse(data.ToString(), out result))
            {
                return false;
            }
            return result;
        }

        public static string ToString(object data)
        {
            if (data != null && data != DBNull.Value)
            {
                return data.ToString();
            }
            return string.Empty;
        }

        public static Guid ToGuid(object data)
        {
            Guid toData = Guid.Empty;
            if (string.IsNullOrEmpty(ConvertHelper.ToString(data)))
            {
                return Guid.Empty;
            }

            if (data.ToString().Length >= 36)
            {
                try
                {
                    return new Guid(ConvertHelper.ToString(data));

                }
                catch (Exception ex)
                {
                    return Guid.Empty;
                }
            }
            return Guid.Empty;
        }

        public static bool TryParseToGuid(object data, out Guid toData)
        {
            toData = Guid.Empty;
            if (string.IsNullOrEmpty(ConvertHelper.ToString(data)))
            {
                return false;
            }

            if (data.ToString().Length >= 36)
            {
                try
                {
                    toData = new Guid(ConvertHelper.ToString(data));
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            return false;
        }

        public static DateTime ToDateTime(object data)
        {
            if (data != null && !string.IsNullOrEmpty(data.ToString()))
            {
                DateTime result = TypeValues.DateTimeDefaultValue;
                if (DateTime.TryParse(data.ToString(), out result))
                {
                    return result;
                }
            }
            return TypeValues.DateTimeDefaultValue;
        }

        public static int ToInt(object data)
        {
            int result = -1;
            if (data == null || data == DBNull.Value || !int.TryParse(data.ToString(), out result))
            {
                return -1;
            }
            return result;
        }

        public static long ToLong(object data)
        {
            long result = -1;
            if (data == null || data == DBNull.Value || !long.TryParse(data.ToString(), out result))
            {
                return -1;
            }
            return result;
        }

        public static decimal ToDecimal(object data)
        {
            decimal result = -1;
            if (data == null || data == DBNull.Value || !decimal.TryParse(data.ToString(), out result))
            {
                return -1;
            }
            return result;
        }

        public static float ToFloat(object data)
        {
            float result = -1;
            if (data == null || data == DBNull.Value || !float.TryParse(data.ToString(), out result))
            {
                return -1;
            }
            return result;
        }

        public static short ToShort(object data)
        {
            short result = -1;
            if (data == null || data == DBNull.Value || !short.TryParse(data.ToString(), out result))
            {
                return -1;
            }
            return result;
        }

        public static double ToDouble(object data)
        {
            double result = -1;
            if (data == null || data == DBNull.Value || !double.TryParse(data.ToString(), out result))
            {
                return -1;
            }
            return result;
        }

        public static object[] ToObjectArray(object data)
        {
            object[] array = null;
            if (data == null || data == DBNull.Value || !(data.GetType() == typeof(object[])))
            {
                return array;
            }
            return (object[])data;
        }


        public static object ParseEnum(Type enumType, object data)
        {
            if (data == null || data == DBNull.Value)
            {
                return null;
            }

            return Enum.Parse(enumType, ToString(data));
        }


    }

}
